热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#|BitConverter。图注32方法

C#|BitConverter。图注32方法原文:htt

C# | BitConverter。图注 32 方法

原文:https://www . geesforgeks . org/c-sharp-bit converter-touint 32-method/

此方法用于返回从字节数组中指定位置的四个字节转换而来的 32 位无符号整数。
语法:

public static uint ToUInt32 (byte[] value, int startIndex);

参数:

值:是字节数组。
起始指数:内的起始位置。

返回值:这个方法返回一个 32 位无符号整数,由 startIndex 开始的四个字节组成。
T3】例外:T5】


  • 参数异常:如果 startIndex 大于或等于值的长度减 3,并且小于或等于值的长度减 1。

  • 参数空异常:如果为空。

  • argumentoutofrangerexception:如果 startIndex 小于零或大于值的长度减 1。

以下程序说明了位转换器的使用。图注 32 方法:
T3】例 1:T5】

c sharp . c sharp . c sharp . c sharp


// C# program to demonstrate
// BitConverter.ToUInt32()
// Method
using System;
class GFG {
    // Main Method
    public static void Main()
    {
        try {
            // Define an array of byte values.
            byte[] bytes = {32, 0, 0, 42, 0, 65,
                            0, 125, 0, 197, 0,
                            168, 3, 41, 4, 125,
                            32};
            // Display the values of the myArr.
            Console.Write("Initial Array: ");
            // calling the PrintIndexAndValues()
            // method to print
            PrintIndexAndValues(bytes);
            // print char value
            Console.WriteLine("index     byte Array"+            
                            "           uint value");
            for (int index = 0; index < bytes.Length - 1;
                                    index = index + 4) {
                uint values = BitConverter.ToUInt32(bytes, index);
                Console.WriteLine("{0}     {1}         {2}",
                       index, BitConverter.ToString(bytes,
                                        index, 4),values);
            }
        }
        catch (ArgumentNullException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentOutOfRangeException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(byte[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
            Console.Write("{0} ", myArr[i]);
        }
        Console.WriteLine();
        Console.WriteLine();
    }
}

Output: 

Initial Array: 32 0 0 42 0 65 0 125 0 197 0 168 3 41 4 125 32
index byte Array uint value
0 20-00-00-2A 704643104
4 00-41-00-7D 2097168640
8 00-C5-00-A8 2818622720
12 03-29-04-7D 2097424643

例 2:议论文异常

c sharp . c sharp . c sharp . c sharp


// C# program to demonstrate
// BitConverter.ToUInt32(Byte[], Int32);
// Method
using System;
class GFG {
    // Main Method
    public static void Main()
    {
        try {
            // Define an array of byte values.
            byte[] bytes = {32, 0, 0, 42, 0, 65,
                              0, 125, 0, 197, 0,
                            168, 3, 41, 4, 125};
            // Display the values of the myArr.
            Console.Write("Initial Array: ");
            // calling the PrintIndexAndValues()
            // method to print
            PrintIndexAndValues(bytes);
            // print char value
            Console.WriteLine("index     byte Array"+            
                            "           uint value");
            for (int index = 1; index < bytes.Length - 2;
                                    index = index + 4) {
                if (index == bytes.Length - 3) {
                    Console.WriteLine();
                    Console.WriteLine("startindex is {0} which is equals to "
                                     + "the length of Array minus 3.",index);
                    uint values = BitConverter.ToUInt32(bytes, index);
                    Console.WriteLine(" {0}     {1}         {2}",
                           index, BitConverter.ToString(bytes,
                                            index, 4),values);
                }
                else {
                    uint values = BitConverter.ToUInt32(bytes, index);
                    Console.WriteLine(" {0}     {1}         {2}",
                           index, BitConverter.ToString(bytes,
                                           index, 4), values);
                }
            }
        }
        catch (ArgumentNullException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentOutOfRangeException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(byte[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
            Console.Write("{0} ", myArr[i]);
        }
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("initial Array in string: {0} ",
                              BitConverter.ToString(myArr));
        Console.WriteLine();
    }
}

Output

Initial Array: 32 0 0 42 0 65 0 125 0 197 0 168 3 41 4 125
initial Array in string: 20-00-00-2A-00-41-00-7D-00-C5-00-A8-03-29-04-7D
index byte Array uint value
1 00-00-2A-00 2752512
5 41-00-7D-00 8192065
9 C5-00-A8-03 61341893
startindex is 13 which is equals to the length of Array minus 3.
Exception Thrown: System.ArgumentException

例 3:argumentout of rangeexception

c sharp . c sharp . c sharp . c sharp


// C# program to demonstrate
// BitConverter.ToUInt32(Byte[], Int32);
// Method
using System;
class GFG {
    // Main Method
    public static void Main()
    {
        try {
            // Define an array of byte values.
            byte[] bytes = {32, 0, 0, 42, 0,
                         65, 0, 125, 0, 197,
                     0, 168, 3, 41, 4, 125};
            // Display the values of the myArr.
            Console.Write("Initial Array: ");
            // calling the PrintIndexAndValues()
            // method to print
            PrintIndexAndValues(bytes);
             // print value
            Console.WriteLine("index     byte Array"+           
                            "           uint value");
            for (int index = 0; index < bytes.Length + 1;
                                    index = index + 4) {
                if (index == bytes.Length) {
                    Console.WriteLine();
                    Console.WriteLine("startindex is {0} which is greater than "
                                       + "the length of Array minus 1.", index);
                    uint values = BitConverter.ToUInt16(bytes, index);
                    Console.WriteLine(" {0}     {1}         {2}",
                           index, BitConverter.ToString(bytes,
                                           index, 4), values);
                }
                else {
                    ushort values = BitConverter.ToUInt16(bytes, index);
                    Console.WriteLine(" {0}     {1}         {2}",
                           index, BitConverter.ToString(bytes,
                                            index, 4), values);
                }
            }
        }
        catch (ArgumentNullException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentOutOfRangeException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(byte[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
            Console.Write("{0} ", myArr[i]);
        }
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("initial Array in string: {0} ",
                           BitConverter.ToString(myArr));
        Console.WriteLine();
    }
}

Output

Initial Array: 32 0 0 42 0 65 0 125 0 197 0 168 3 41 4 125
initial Array in string: 20-00-00-2A-00-41-00-7D-00-C5-00-A8-03-29-04-7D
index byte Array uint value
0 20-00-00-2A 32
4 00-41-00-7D 16640
8 00-C5-00-A8 50432
12 03-29-04-7D 10499
startindex is 16 which is greater than the length of Array minus 1.
Exception Thrown: System.ArgumentOutOfRangeException

例 4:ArgumentNullException

c sharp . c sharp . c sharp . c sharp


// C# program to demonstrate
// BitConverter.ToUInt32(Byte[], Int32);
// Method
using System;
class GFG {
    // Main Method
    public static void Main()
    {
        try {
            // Define an array of byte values.
            byte[] bytes = null;
            // get the uint value
            uint values = BitConverter.ToUInt32(bytes, 0);
            Console.Write("{0}", values);
        }
        catch (ArgumentNullException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentOutOfRangeException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

Output: 

Exception Thrown: System.ArgumentNullException

参考:


  • https://docs . Microsoft . com/en-us/dotnet/API/system . bit converter . touint 32?视图=netframework-4.7.2


推荐阅读
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • 本文介绍了在Java中gt、gtgt、gtgtgt和lt之间的区别。通过解释符号的含义和使用例子,帮助读者理解这些符号在二进制表示和移位操作中的作用。同时,文章还提到了负数的补码表示和移位操作的限制。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
author-avatar
鬼蕾12_950_709
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有